package com.longcai.piaojia.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.MyCollectionGet;
import com.longcai.piaojia.fragment.FragmentMyCollection;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.util.UtilToast;

import java.util.List;

/**
 * Created by Administrator on 2016/8/5.
 */
public class MyCollectionListAdapter extends ArrayAdapter<MyCollectionGet.MyCollection> {

    private Context context;
    private LayoutInflater layoutInflater;
    private DeleteItemCallback deleteItemCallback;

    public MyCollectionListAdapter(Context context, List<MyCollectionGet.MyCollection> objects, FragmentMyCollection fragmentMyCollection) {

        super(context, 0, objects);

        this.context = context;

        layoutInflater = ((Activity) context).getLayoutInflater();

        deleteItemCallback = fragmentMyCollection;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        MyCollectionGet.MyCollection myCollection = getItem(position);

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_mycollection_list, null)));

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        if (position == 0) {

            viewHolder.item_mycollection_out_ll.setPadding(0, ScaleScreenHelperFactory.getInstance().getWidthHeight(20), 0, 0);
        }


        viewHolder.item_mycollection_title_tv.setText(myCollection.info.title);
        viewHolder.item_mycollection_price_tv.setText(myCollection.info.price_low + context.getResources().getString(R.string.goods_info_symbols_text) + myCollection.info.price_high + context.getResources().getString(R.string.goods_info_money_text));

        switch (Integer.parseInt(myCollection.info.is_seat)) {

            case 0:

                viewHolder.item_mycollection_is_seat_tv.setVisibility(View.GONE);
                viewHolder.item_mycollection_undetermined_tv.setVisibility(View.GONE);
                break;

            case 1:

                viewHolder.item_mycollection_is_seat_tv.setVisibility(View.VISIBLE);
                viewHolder.item_mycollection_undetermined_tv.setVisibility(View.GONE);
                break;

            case 2:

                viewHolder.item_mycollection_is_seat_tv.setVisibility(View.GONE);
                viewHolder.item_mycollection_undetermined_tv.setVisibility(View.VISIBLE);
                break;
        }

        if (myCollection.info.status.equals("0")) {

            viewHolder.item_mycollection_cao_iv.setVisibility(View.VISIBLE);

        } else {

            viewHolder.item_mycollection_cao_iv.setVisibility(View.GONE);

        }


        switch (Integer.parseInt(myCollection.info.status)){

            case 0:

                viewHolder.item_mycollection_ordering_tv.setTextColor(ContextCompat.getColor(context, R.color.hint_color));
                viewHolder.item_mycollection_ordering_tv.setBackgroundResource(R.drawable.item_mycollection_nostart_text_bg);
                viewHolder.item_mycollection_ordering_tv.setText(context.getResources().getString(R.string.goods_info_status_notstart_text));

                break;
            case 1:

                viewHolder.item_mycollection_ordering_tv.setTextColor(ContextCompat.getColor(context, R.color.item_mycollection_money_text_color));
                viewHolder.item_mycollection_ordering_tv.setBackgroundResource(R.drawable.item_mycollection_ordering_text_bg);
                viewHolder.item_mycollection_ordering_tv.setText(context.getResources().getString(R.string.goods_info_status_ordering_text));

                break;
            case 2:

                viewHolder.item_mycollection_ordering_tv.setTextColor(ContextCompat.getColor(context, R.color.item_mycollection_sale_text_color));
                viewHolder.item_mycollection_ordering_tv.setBackgroundResource(R.drawable.item_mycollection_sale_text_bg);
                viewHolder.item_mycollection_ordering_tv.setText(context.getResources().getString(R.string.goods_info_status_selling_text));

                break;
            case 3:

                viewHolder.item_mycollection_ordering_tv.setTextColor(ContextCompat.getColor(context, R.color.hint_color));
                viewHolder.item_mycollection_ordering_tv.setBackgroundResource(R.drawable.item_mycollection_nostart_text_bg);
                viewHolder.item_mycollection_ordering_tv.setText(context.getResources().getString(R.string.goods_info_status_sellout_text));

                break;
        }

        viewHolder.item_mycollection_date_tv.setText(myCollection.posttime);
        viewHolder.item_mycollection_address_tv.setText(myCollection.info.place_name);

        GlideLoader.getInstance().get(context, myCollection.info.picurl, viewHolder.item_mycollection_iv, R.mipmap.goods);

        final View finalConvertView = convertView;
        viewHolder.item_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                deleteItemCallback.deleteItem(position, finalConvertView);

            }
        });


        return convertView;

    }

    private class ViewHolder {

        @BoundView(R.id.item_mycollection_iv) private ImageView item_mycollection_iv;
        @BoundView(R.id.item_mycollection_cao_iv) private ImageView item_mycollection_cao_iv;
        @BoundView(R.id.item_mycollection_price_tv) private TextView item_mycollection_price_tv;
        @BoundView(R.id.item_mycollection_is_seat_tv) private TextView item_mycollection_is_seat_tv;
        @BoundView(R.id.item_mycollection_ordering_tv) private TextView item_mycollection_ordering_tv;
        @BoundView(R.id.item_mycollection_undetermined_tv) private TextView item_mycollection_undetermined_tv;
        @BoundView(R.id.item_mycollection_address_tv) private TextView item_mycollection_address_tv;
        @BoundView(R.id.item_mycollection_date_tv) private TextView item_mycollection_date_tv;
        @BoundView(R.id.item_mycollection_title_tv) private TextView item_mycollection_title_tv;
        @BoundView(R.id.item_right) private LinearLayout item_right;
        @BoundView(R.id.item_mycollection_out_ll) private LinearLayout item_mycollection_out_ll;

    }


    public interface DeleteItemCallback{
        void deleteItem(int pos, View view);
    }


}
